﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WEB.Core.Controls;
using Ext.Net;
using WEB.BLL.Entities;
using WEB.BLL.Services;
using WEB.Core.Util;
using System.Web.Security;

namespace WEB.APP
{
    public partial class Login : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnLogin_Click(object sender, DirectEventArgs e)
        {
            try
            {
                string userName = txtUsername.Text.Trim();
                string password = txtPassword.Text.Trim();
                bool remember = chkRemeber.Checked;
                UserEntity tmpuserEntity = new UserEntity();
                tmpuserEntity.UserName = userName;
                UserService userService = UserService.GetInstance();
                UserEntity userEntity = new UserEntity();
                userEntity = userService.UserLoadCheckAcitve(tmpuserEntity);
                if (null != userEntity)
                {
                    if (userEntity.IsActive == true)
                    {
                        if (null != userEntity || string.Empty != userEntity.UserName)
                        {
                            string encrypPassword = Encryption.StringToMD5Hash(password).ToLower();
                            if (encrypPassword.Equals(userEntity.Password.ToLower()))
                            {
                                string returnUrl = Request.QueryString["ReturnUrl"];
                                FormsAuthentication.RedirectFromLoginPage(userName, remember);
                                //LogActivity(userEntity);
                                if (returnUrl == null) returnUrl = "~/Default.aspx";

                                Response.Redirect(returnUrl);
                                return;
                            }
                            lblError.Text = "Password is incorrect";
                            lblError.Show();
                            return;
                        }
                    }
                    lblError.Text = "This user has not activated";
                    lblError.Show();
                    return;
                }
                lblError.Text = "Login failed";
                lblError.Show();

            }
            catch (Exception ex)
            {
                Notification(ex.Message);
            }
        }
        //private void LogActivity(UserEntity userEntity)
        //{
        //    try
        //    {
        //        ActivityLogEntity log = new ActivityLogEntity();
        //        log.Comment = string.Format("Người dùng {0} đăng nhập lúc ({1})", userEntity.UserName, DateTime.Now.ToShortTimeString());
        //        log.UserId = userEntity.EmployeeId;
        //        log.CreatedBy = userEntity.EmployeeId;
        //        log.UpdatedBy = userEntity.EmployeeId;
        //        log.CreatedDate = DateTime.Now;
        //        log.UpdatedDate = DateTime.Now;
        //        ActivityLogService logService = ActivityLogService.GetInstance();
        //        logService.Insert(log);
        //    }
        //    catch (Exception ex)
        //    {
        //        Notification(ex.Message);
        //    }
        //}
        public void Notification(string errorCode)
        {

            string title = "Notification";
            string message = "";
            switch (errorCode)
            {
                case "0x000000": message = "Unable to connect to data"; break;
                case "0x000002": message = "No files found"; break;
                default: message = errorCode; break;
            }
            Ext.Net.Notification.Show(new NotificationConfig
            {
                Title = title,
                ShowPin = true,
                Html = message
            });
        }

    }
}
